package com.lb.pdm.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lb.pdm.common.constants.RedisConstants;
import com.lb.pdm.common.result.GraceJSONResult;
import com.lb.pdm.entity.CertificateInfo;
import com.lb.pdm.entity.SysUser;
import com.lb.pdm.mapper.CertificateInfoMapper;
import com.lb.pdm.service.CertificateInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 证件信息表 服务实现类
 * </p>
 *
 * @author pdm
 * @since 2025-10-29
 */
@Service
public class CertificateInfoServiceImpl extends ServiceImpl<CertificateInfoMapper, CertificateInfo> implements CertificateInfoService {
    @Autowired
    private CertificateInfoMapper certificateInfoMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public GraceJSONResult cancellation(String certificateNumber, String token) {
        LambdaQueryWrapper<CertificateInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CertificateInfo::getCertificateNumber, certificateNumber);
        CertificateInfo certificateInfo = certificateInfoMapper.selectOne(queryWrapper);
        if (certificateInfo != null) {
            certificateInfo.setStatus((byte)4);
            certificateInfoMapper.updateById(certificateInfo);

            String sysUserString = redisTemplate.opsForValue().get(RedisConstants.USER_TOKEN_PREFIX + token);
            SysUser sysUser = JSON.parseObject(sysUserString, SysUser.class);
            certificateInfo.setUpdateBy(sysUser.getId());
        }

        return GraceJSONResult.ok();
    }
}
